package com.jumi.microservice.rocketmq;

import com.aliyun.mq.http.MQClient;
import com.aliyun.mq.http.MQProducer;
import com.jumi.microservice.common.exception.GlobalExceptionHandler;
import com.jumi.microservice.config.RocketMqConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Description rocketMq-http生产者
 * @ClassName RocketMqProducer
 * @Author Abel Yang
 * @Date 2020/10/24/024 9:46
 **/
@Configuration
public class RocketMqHttpProducer {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Resource
    private RocketMqConfig rocketMqConfig;

    @Bean
    public MQProducer getProducerHttp() {
        log.info(rocketMqConfig.toString());
        log.info("-----------rocketMq-httpProducer 开始创建----------------------");

        MQProducer producer;
        MQClient mqClient = new MQClient(
                rocketMqConfig.getEndpoint(),
                rocketMqConfig.getAccessKey(),
                rocketMqConfig.getSecretKey()
        );
        if (rocketMqConfig.getInstanceId() != null && !rocketMqConfig.getInstanceId().equals("")) {
            producer = mqClient.getProducer(rocketMqConfig.getInstanceId(), rocketMqConfig.getTopic());
        } else {
            producer = mqClient.getProducer(rocketMqConfig.getTopic());
        }
        log.info("-----------rocketMq-httpProducer 创建完毕----------------------");
        return producer;
    }
}
